var _oPopupMenu = null;
var _oBaseDate = null;


function Refresh()
{
oTable = document.getElementById("LoadingTable");
if (oTable != null)
{
oTable.style.display = "block";
}
oTable = document.getElementById("TimeSheetTable");
if (oTable != null)
{
oTable.style.display = "none";
}
for (iRangeIndex = iMaxRange - 1; iRangeIndex >= 0; iRangeIndex--)
{

oTable.deleteRow(iRangeIndex);
}

iMaxRange = 1;
OnPageLoad();
}

function CommitForm()
{
var iRangeIndex;
var sRuleType;
var oStartTime;
var Effort = null;
var Duration;
var Data = "<rules>";

for (iRangeIndex = 0; iRangeIndex < iMaxRange; iRangeIndex++)
{
sRuleType = document.getElementById("Type" + iRangeIndex).value;
if (sRuleType == "effort")
{

if (!ValidCapacityRange(iRangeIndex, 0.0001, 999999999))
{
return false;
}
else
{
Effort = capacityFormValue(iRangeIndex);
}
}

oStartTime = GetStartTimeSelect(iRangeIndex);
Duration = GetRangeDuration(iRangeIndex);

Data += "<rule><type>" + sRuleType + "</type><start>" + FormatUtcDate(oStartTime) + "</start><effort>" + Effort + "</effort><duration>" + Duration + "</duration></rule>";
}
Data += "</rules>";
TimeSheetForm.clientData.value = Data;
return true;
}



function GetCurrentWorkingHours()
{
var oWorkingHours = new Object();

oWorkingHours.ScheduledStart	= GetStartTimeSelect(0);
oWorkingHours.ScheduledEnd		= GetEndTimeSelect(iMaxRange-1);
return oWorkingHours;
}



function SplitRule(iRangeIndex, SplitTime, SplitEffort)
{
var oTimeSheetTable = document.getElementById("TimeSheetTable");
var i;
var ClassName;
var DecorClassName;


if (iRangeIndex == iMaxRange - 1)
{
NewRow = oTimeSheetTable.rows[RuleRowNum + iRangeIndex];
AssignRowCellsStyles(NewRow, iRangeIndex);
}
DecorClassName = "cellBottom";



for (i = iMaxRange - 1; i >= (iRangeIndex + 1); i--)
RenumberTags(i, i + 1);

iMaxRange++;

RenderRow('effort', iRangeIndex, DecorClassName, SplitTime, SplitEffort);


SetMenuItemState(iRangeIndex);
SetMenuItemState(iRangeIndex + 1);
}


function InsertRule(iRangeIndex, oBreakTime, BreakDuration)
{
var oTimeSheetTable = document.getElementById("TimeSheetTable");
var i;
var ClassName;
var DecorClassName;
var forceEffort;

forceEffort = document.getElementById("Effort" + iRangeIndex).value;

if (iRangeIndex == iMaxRange - 1)
{
NewRow = oTimeSheetTable.rows[RuleRowNum + iRangeIndex];
AssignRowCellsStyles(NewRow, iRangeIndex);
}
DecorClassName = "cellBottom";



for (i = iMaxRange - 1; i >= (iRangeIndex + 1); i--)
{
RenumberTags(i, i + 1);
}
iMaxRange++;

RenderRow('break', iRangeIndex, DecorClassName, oBreakTime);

iRangeIndex++;


for (i = iMaxRange - 1; i >= (iRangeIndex + 1); i--)
{
RenumberTags(i, i + 1);
}
iMaxRange++;

var oForceSplitTime = GetBreakTime(iRangeIndex, BreakDuration);
RenderRow('effort', iRangeIndex, DecorClassName, oForceSplitTime, forceEffort);
}


function RenderRow(RowType, iRangeIndex, DecorClassName, oForcedSplitTime, ForcedEffort)
{
var oTimeSheetTable = document.getElementById("TimeSheetTable");
var NewRow;
var oTd;
var innerHtml;
var OldStartTime;
var OldEndTime;
var SplitTime;
var Effort;
var inputType = EnableEffort ? "text" : "hidden";

NewRow = oTimeSheetTable.insertRow(RuleRowNum + iRangeIndex + 1);
NewRow.className = RowType + "CellTr";
NewRow.id = "Row" + (iRangeIndex + 1);

oTd = document.createElement("TD");
oTd.className = "ruleTypeTd " + DecorClassName + "Left";
oTd.noWrap = true;
if (RowType == 'effort')
{
oTd.innerHTML =  SplitRuleName + RenderInput("hidden", "effort", "Type" + (iRangeIndex + 1));
NewRow.appendChild(oTd);

if (iRangeIndex > -1)
{

if (document.getElementById("Effort" + iRangeIndex))
{
Effort = document.getElementById("Effort" + iRangeIndex).value;
}
else
{
Effort = DefaultEffort;
}

if (ForcedEffort)
{
Effort = ForcedEffort;
}
}
else
{
Effort = DefaultEffort;
}
oTd = document.createElement("TD");
oTd.className = "effortTd " + DecorClassName + "Left";
if (inputType == "hidden")
oTd.style.width = 0;
oTd.align = "center";

oTd.innerHTML = RenderInput(inputType, Effort, "Effort" + (iRangeIndex + 1), "OnCapacityLostFocus(" + (iRangeIndex + 1) + ");", null, EffortRuleName);
NewRow.appendChild(oTd);
}
else
{
oTd.innerHTML = InsertRuleName + RenderInput("hidden", "break", "Type" + (iRangeIndex + 1));
NewRow.appendChild(oTd);

oTd = document.createElement("TD");
oTd.className = "effortTd " + DecorClassName + "Left";
oTd.innerHTML = "&nbsp;";
NewRow.appendChild(oTd);
}

if (iRangeIndex == -1)
{

OldStartTime = new Date(TemplateStartTime.toString());
OldEndTime   = new Date(TemplateEndTime.toString());
SplitTime = new Date(TemplateStartTime.toString());
}
else
{
OldStartTime = GetStartTimeSelect(iRangeIndex);
OldEndTime   = GetEndTimeSelect(iRangeIndex);
if (!oForcedSplitTime)
{
SplitTime = GetSplitTime(OldStartTime, OldEndTime);
}
else
{
SplitTime = oForcedSplitTime;
}
}


oTd = RenderTimeTd("StartTime", iRangeIndex + 1, SplitTime, "startTd " + DecorClassName + "Left");
NewRow.appendChild(oTd);


if (iRangeIndex == -1)
{
oTd = RenderTimeTd("EndTime", iRangeIndex + 1, TemplateEndTime, "endTd " + DecorClassName + "Left");
}
else
{

oTd = RenderTimeTd("EndTime", iRangeIndex + 1, OldEndTime, "endTd " + DecorClassName + "Left");
SetEndTimeSelect(iRangeIndex, SplitTime);
}
NewRow.appendChild(oTd);


oTd = document.createElement("TD");
oTd.className = "actionsTd " + DecorClassName + "LeftRight";
oTd.id = "actionsTd" + (iRangeIndex + 1);
if (RowType == "effort")
{
innerHtml = "<table cellspacing=\"0\" cellpadding=\"0\"><tr><td>";
innerHtml += RenderButton("InsertRule(" + (iRangeIndex + 1) + ");", "InsertButton" + (iRangeIndex + 1), InsertActionName, "actionButton", "block");
innerHtml += "</td><td>";
innerHtml += RenderImage("/_imgs/menuButton", LOCID_MENUBUTTON_ALTTAG, "onMenuButton(" + (iRangeIndex + 1) + ");", "MenuButton" + (iRangeIndex + 1), "menuButton", "none");
innerHtml += "</td></tr></table>";
}
else
{
innerHtml = RenderButton("MergeInserts(" + (iRangeIndex + 1) + ");", "MergeButton" + (iRangeIndex + 1), MergeActionName, "actionButton", "block");
}
oTd.innerHTML = innerHtml;
NewRow.appendChild(oTd);


oTd = document.createElement("TD");
if (RowType == "effort")
{
innerHtml = RenderMenuItem("SplitRule(" + (iRangeIndex + 1) + ");", "SplitMenuItem" + (iRangeIndex + 1), SplitActionName, "block");
if (iRangeIndex > -1)
{
innerHtml += RenderMenuItem("MergeRow(" + (iRangeIndex + 1) + ");", "MergeMenuItem" + (iRangeIndex + 1), MergeActionName, "block");
}
oTd.innerHTML = innerHtml;
}
oTd.style.display = "none";
NewRow.appendChild(oTd);


if (EnableEffort)
{
var oImg = document.getElementById("MenuButton" + (iRangeIndex + 1));
ToggleImage(null, oImg);
}
}



function SetMenuItemState(iRangeIndex)
{
var RangeStart;
var RangeEnd;
var SplitMenuItem;
var InsertButton;
var oStartDateTime;
var oEndDateTime;
var RangeDiff;
var MakeDisabled = (iMaxRange >= MAX_ROWS);

if (MakeDisabled || (IsNull(iRangeIndex)))
{
RangeStart = 0;
RangeEnd = iMaxRange - 1;
}
else
{
RangeStart = iRangeIndex;
RangeEnd = iRangeIndex;
}

for (iRangeIndex = RangeStart; iRangeIndex <= RangeEnd; iRangeIndex++)
{
SplitMenuItem = document.getElementById("SplitMenuItem" + iRangeIndex);
InsertButton = document.getElementById("InsertButton" + iRangeIndex);

if (!SplitMenuItem)
continue;

if (MakeDisabled)
{
SplitMenuItem.disabled = "true";
InsertButton.disabled = "true";
}
else
{
oStartDateTime = GetStartTimeSelect(iRangeIndex);
oEndDateTime = GetEndTimeSelect(iRangeIndex);
RangeDiff = (oEndDateTime - oStartDateTime)/60000;


SplitMenuItem.disabled = (TemplateApptsStartEvery >= RangeDiff);
InsertButton.disabled = (TemplateApptsStartEvery >= RangeDiff/2);
}
}

HidePopup();
}

function IsNextRowValidForMerge(iRangeIndex)
{
var prevRowIndex;
if (iRangeIndex == 0)
{
iRangeIndex = 1;
}
else if (iRangeIndex < 0)
{
return false;
}
prevRowIndex = iRangeIndex - 1;

if (document.getElementById("Type" + prevRowIndex).value == "effort")
{
return ((!EnableEffort) &&
(iRangeIndex < iMaxRange) &&
(document.getElementById("Type" + iRangeIndex).value == "effort") &&
(document.getElementById("Effort" + iRangeIndex).value == document.getElementById("Effort" + prevRowIndex).value));
}
else
{

return ((!EnableEffort) &&
(iRangeIndex < iMaxRange) &&
(document.getElementById("Type" + iRangeIndex).value == "break"));

}
}


function MergeValidForMergeRow(iRangeIndex)
{
while (IsNextRowValidForMerge(iRangeIndex))
{
MergeRow(iRangeIndex);
}
}


function MergeInserts(iRangeIndex)
{

MergeRow(iRangeIndex);

MergeValidForMergeRow(iRangeIndex);
}


function MergeRow(iRangeIndex)
{
var oTimeSheetTable;
var MergedEndTime;
var i = 0;
var ClassName;
var NewRow;


if (iRangeIndex == 0)
{
iRangeIndex = 1;
}

if (iRangeIndex > iMaxRange - 1)
{
return;
}

MergedEndTime = GetEndTimeSelect(iRangeIndex);

if (iRangeIndex > 0)
{
SetEndTimeSelect(iRangeIndex - 1, MergedEndTime);
}


oTimeSheetTable = document.getElementById("TimeSheetTable");
while (oTimeSheetTable.rows[i].id != "Row" + iRangeIndex)
{
i++;
}
oTimeSheetTable.deleteRow(i);


iMaxRange--;



for (i = (iRangeIndex + 1); i <= iMaxRange; i++)
{
RenumberTags(i, i - 1);
}

if (iRangeIndex == iMaxRange)
{
NewRow = oTimeSheetTable.rows[RuleRowNum + iMaxRange - 1];
AssignRowCellsStyles(NewRow, iRangeIndex - 1);
}
formUpdateElements(iRangeIndex - 1);
}

function AssignRowCellsStyles(oRow, iRangeIndex)
{
for (i = 0; i < oRow.cells.length; i++)
{
ClassName = oRow.children[i].className;
ClassName = ClassName.substring(0, ClassName.indexOf("cell") + 4);


if (iRangeIndex == 1)
{
ClassName += "Top";
}

ClassName += "Bottom";
ClassName += "Left";


if (i == (oRow.cells.length - 1))
{
ClassName += "Right";
}
oRow.cells[i].className = ClassName;
}
}


function RenumberTags(OldRangeIndex, NewRangeIndex)
{
var oButton;
var oImage;
var oMenuItem;
var oElement;

if (document.getElementById("Type" + OldRangeIndex).value == "break")
{
oButton = document.getElementById("MergeButton" + OldRangeIndex);
oButton.onclick = "MergeInserts(" + NewRangeIndex + ")";
oButton.id = "MergeButton" + NewRangeIndex;
oButton.outerHTML = oButton.outerHTML;
}
else
{
oButton = document.getElementById("InsertButton" + OldRangeIndex);
oButton.onclick = "InsertRule(" + NewRangeIndex + ")";
oButton.id = "InsertButton" + NewRangeIndex;
oButton.outerHTML = oButton.outerHTML;

oImage = document.getElementById("MenuButton" + OldRangeIndex);
oImage.onclick = "onMenuButton(" + NewRangeIndex + ")";
oImage.id = "MenuButton" + NewRangeIndex;
oImage.outerHTML = oImage.outerHTML;

oMenuItem = document.getElementById("SplitMenuItem" + OldRangeIndex);
oMenuItem.onclick = "SplitRule(" + NewRangeIndex + ")";
oMenuItem.id = "SplitMenuItem" + NewRangeIndex;
oMenuItem.outerHTML = oMenuItem.outerHTML;

oMenuItem = document.getElementById("MergeMenuItem" + OldRangeIndex);
oMenuItem.onclick = "MergeRow(" + NewRangeIndex + ")";
oMenuItem.id = "MergeMenuItem" + NewRangeIndex;
oMenuItem.outerHTML = oMenuItem.outerHTML;

oElement = document.getElementById("Effort" + OldRangeIndex);
oElement.id = "Effort" + NewRangeIndex;

oElement.onblur = "OnCapacityLostFocus('" + NewRangeIndex + "');";
oElement.outerHTML = oElement.outerHTML;
}

RenumberStartTimeSelect(OldRangeIndex, NewRangeIndex);

oElement = document.getElementById("StartTimeTD" + OldRangeIndex);
oElement.id = "StartTimeTD" + NewRangeIndex;

RenumberEndTimeSelect(OldRangeIndex, NewRangeIndex);

oElement = document.getElementById("EndTimeTD" + OldRangeIndex);
oElement.id = "EndTimeTD" + NewRangeIndex;

oElement = document.getElementById("Type" + OldRangeIndex);
oElement.id = "Type" + NewRangeIndex;

oElement = document.getElementById("Row" + OldRangeIndex);
oElement.id = "Row" + NewRangeIndex;
}


function OnPageLoad()
{
var oTable;


RenderRow('effort', -1, 'cellTopBottom');


_oBaseDate = GetStartTimeSelect(0);


LoadFromXml();

oTable = document.getElementById("LoadingTable");
if (oTable != null)
{
oTable.style.display = "none";
}
oTable = document.getElementById("TimeSheetTable");
if (oTable != null)
{
oTable.style.display = "block";
}
}

function OnMouseOverHeader()
{


}

function OnMouseOutHeader()
{


}

function OnEffortToggle()
{
var oColumnTd;

if (EnableEffort)
{
EnableEffort = false;
oColumnTd = document.getElementById("effortColumn");
EffortColumnHeader = oColumnTd.innerHTML;
oColumnTd.innerHTML = "";
oColumnTd.style.width = 0;
ToggleCapacity("hidden", 0);
}
else
{
EnableEffort = true;
oColumnTd = document.getElementById("effortColumn");
oColumnTd.innerHTML = EffortColumnHeader;
oColumnTd.style.width = null;
ToggleCapacity("text", null);
}
}


function onSelectInitDataValue(id)
{
formUpdateElements();
}

function formUpdateElements(iRangeIndex)
{

SetMenuItemState(iRangeIndex);
OnUpdateTotals();
}

/* Start time changed
1. Start time was changed past end time - resolution
- alert and cancel
2. Start time was changed to overlap earlier ranges (changed row will be last)
- remove overlapped rows (not changed row)
- adjust end time of previous range to new value
3. Start time was changed to overlap later ranges (changed row will be first)
- remove overlapped rows (not changed row)
- *adjust end time of changed row to end time of last removed row
- adjust end time of previous range to new value
4. Start time changed to match end time	(changed row will be first)
- remove current range
- adjust end time of previous range to new value
5. start time was changed, no overlap
- adjust end time of previous range to new value
*/
function OnStartTimeChange(iRangeIndex)
{
var sRangeOverlaps = "";
var sWarnMessage;

var oStartDateTime;
var oEndDateTime;
var oNewDateTime;
var oOriginalValue;
var i;
var bMergeFirst = false;


oNewDateTime = GetStartTimeSelect(iRangeIndex);


if (iRangeIndex > 0)
{
oOriginalValue = GetEndTimeSelect(iRangeIndex - 1);
}
else
{
oOriginalValue = new Date(TemplateStartTime.toString());
oEndDateTime = GetEndTimeSelect(iMaxRange - 1);
if (oOriginalValue >= oEndDateTime)
{
oOriginalValue.setTime(oEndDateTime.getTime());
oOriginalValue.setMinutes(oEndDateTime.getMinutes() - TemplateApptsStartEvery);
}
oEndDateTime.setMinutes(oEndDateTime.getMinutes() - TemplateApptsStartEvery);
if (oNewDateTime > oEndDateTime)
{
var sMsg = StartTimeLater.replace("{0}", TemplateApptsStartEvery);
alert(sMsg);
SetStartTimeSelect(iRangeIndex, oOriginalValue);
formUpdateElements(iRangeIndex);
return;
}
}


for (i = 0; i < iMaxRange; i++)
{
oStartDateTime = GetStartTimeSelect(i);
oEndDateTime = GetEndTimeSelect(i);

if ((i < iRangeIndex) && (oNewDateTime <= oStartDateTime))
{
sRangeOverlaps += FormatShortDateTime(oStartDateTime) + " - " + FormatShortDateTime(oEndDateTime) + "\n";
sWarnMessage = OverlapMessage + "\n" + sRangeOverlaps + MergeMessage;
}
else if ((i == iRangeIndex) && (oNewDateTime.toString() == oEndDateTime.toString()))
{
sRangeOverlaps += FormatShortDateTime(oOriginalValue) + " - " + FormatShortDateTime(oEndDateTime) + "\n";
sWarnMessage = OverlapMessage + "\n" + sRangeOverlaps + MergeMessage;
}
else if ((i > iRangeIndex) && (oNewDateTime >= oStartDateTime))
{
sRangeOverlaps += FormatShortDateTime(oStartDateTime) + " - " + FormatShortDateTime(oEndDateTime) + "\n";
sWarnMessage = OverlapMessage + "\n" + sRangeOverlaps + MergeMessage;
}
else if ((i == iMaxRange - 1) && (oNewDateTime >= oEndDateTime))
{
sRangeOverlaps += FormatShortDateTime(oOriginalValue) + " - " + FormatShortDateTime(oEndDateTime) + "\n";
sWarnMessage = ConflictMessage + "\n" + sRangeOverlaps + MergeMessage;
}
}

if (sRangeOverlaps != "")
{

if (confirm(sWarnMessage))
{

if ((iRangeIndex > 0) && (CheckEndNotCloseToMidnight(oNewDateTime)))
{
SetEndTimeSelect(iRangeIndex - 1, oNewDateTime);
}

for (i = 0; i < iMaxRange; i++)
{
oStartDateTime = GetStartTimeSelect(i);
oEndDateTime = GetEndTimeSelect(i);

if ((i < iRangeIndex) && (oNewDateTime <= oStartDateTime))
{
if (i == 0)
{

MergeRow(iRangeIndex);
bMergeFirst = true;
}
else
{
MergeRow(i);
i--;
}
iRangeIndex--;
}

else if ((i > iRangeIndex) && (oNewDateTime >= oStartDateTime))
{
MergeRow(i);
i--;
}

if ((i == iMaxRange - 1) && (iRangeIndex == iMaxRange - 1) && (oStartDateTime > oEndDateTime))
{

if (CheckEndNotCloseToMidnight(oNewDateTime))
{
oOriginalValue.setTime(oNewDateTime.getTime());
oOriginalValue.setMinutes(oNewDateTime.getMinutes() + TemplateApptsStartEvery);
SetEndTimeSelect(i, oOriginalValue);
}
else
{

var errorString = ErrorInRow.replace("{0}", iRangeIndex);
alert(errorString);
SetStartTimeSelect(iRangeIndex, oOriginalValue);
}
}

else if ((i == iRangeIndex) && ((oStartDateTime.toString() == oEndDateTime.toString()) ||
(!EnableEffort) && (oNewDateTime <= oStartDateTime)))
{
MergeRow(i);
iRangeIndex--;
}
}
}
else
{
SetStartTimeSelect(iRangeIndex, oOriginalValue);
return;
}
}

else if (iRangeIndex > 0 && !bMergeFirst)
{
SetEndTimeSelect(iRangeIndex - 1, oNewDateTime);
formUpdateElements(iRangeIndex - 1);
}

MergeValidForMergeRow(iRangeIndex);

MergeValidForMergeRow(iRangeIndex + 1);

formUpdateElements(iRangeIndex);
}

/* End time changed
1. end time was changed earlier then start time
- alert and revert.
2. end time was changed to overlap earlier ranges (changed row will be last)
- *remove overlapped rows (not first row, but including changed row)
- adjust end time of previous range to new value
- adjust start time of next range to new value
3. end time was changed to overlap later ranges (changed row will be first)
- remove overlapped rows (not changed row)
- adjust start time of next range to new value
4. end time was changed to match start time (changed row will be last)
- remove current range
- adjust start time of next range to new value
5. end time was changed, no overlap
- adjust start time of next range to new value
*/
function OnEndTimeChange(iRangeIndex)
{



window.setTimeout("OnEndTimeChangeInternal(" + iRangeIndex +")", 1);
}
function OnEndTimeChangeInternal(iRangeIndex)
{
var sRangeOverlaps = "";
var sWarnMessage;

var oStartDateTime;
var oEndDateTime;
var oNewDateTime;
var oOriginalValue;
var i;
var bMergeFirst = false;


oNewDateTime = GetEndTimeSelect(iRangeIndex);

if (iRangeIndex + 1 < iMaxRange)
{
oOriginalValue = GetStartTimeSelect(iRangeIndex + 1);
}
else
{
oOriginalValue = new Date(TemplateEndTime.toString());
oStartDateTime = GetStartTimeSelect(0);
if (oOriginalValue <= oStartDateTime)
{
oOriginalValue.setTime(oStartDateTime.getTime());
oOriginalValue.setMinutes(oStartDateTime.getMinutes() + TemplateApptsStartEvery);
}
oStartDateTime.setMinutes(oStartDateTime.getMinutes() + TemplateApptsStartEvery);
if (oNewDateTime < oStartDateTime)
{
var sMsg = EndTimeEarlier.replace("{0}", TemplateApptsStartEvery);
alert(sMsg);
SetEndTimeSelect(iRangeIndex, oOriginalValue);
formUpdateElements(iRangeIndex);
return;
}
}


for (i = 0; i < iMaxRange; i++)
{
oStartDateTime = GetStartTimeSelect(i);
oEndDateTime = GetEndTimeSelect(i);

if ((i <= iRangeIndex) && (oNewDateTime < oEndDateTime))
{
sRangeOverlaps += FormatShortDateTime(oStartDateTime) + " - " + FormatShortDateTime(oEndDateTime) + "\n";
sWarnMessage = OverlapMessage + "\n" + sRangeOverlaps + MergeMessage;
}
else if ((i == iRangeIndex) && (oNewDateTime.toString() == oStartDateTime.toString()))
{
sRangeOverlaps += FormatShortDateTime(oStartDateTime) + " - " + FormatShortDateTime(oOriginalValue) + "\n";
sWarnMessage = OverlapMessage + "\n" + sRangeOverlaps + MergeMessage;
}
else if ((i > iRangeIndex) && (oNewDateTime >= oEndDateTime))
{
sRangeOverlaps += FormatShortDateTime(oStartDateTime) + " - " + FormatShortDateTime(oEndDateTime) + "\n";
sWarnMessage = OverlapMessage + "\n" + sRangeOverlaps + MergeMessage;
}
else if ((i == 0) && (oNewDateTime <= oStartDateTime))
{
sRangeOverlaps += FormatShortDateTime(oStartDateTime) + " - " + FormatShortDateTime(oOriginalValue) + "\n";
sWarnMessage = ConflictMessage + "\n" + sRangeOverlaps + MergeMessage;
}
}

if (sRangeOverlaps != "")
{

if (confirm(sWarnMessage))
{


if ((iRangeIndex + 1 < iMaxRange) && (CheckStartNotCloseToMidnight(oNewDateTime)))
{
SetStartTimeSelect(iRangeIndex + 1, oNewDateTime);
}

for (i = 0; i < iMaxRange; i++)
{
oStartDateTime = GetStartTimeSelect(i);
oEndDateTime = GetEndTimeSelect(i);

if ((i == 0) && (iRangeIndex == 0) && (oStartDateTime >= oEndDateTime))
{
if (CheckStartNotCloseToMidnight(oNewDateTime))
{
oOriginalValue.setTime(oNewDateTime.getTime());
oOriginalValue.setMinutes(oNewDateTime.getMinutes() - TemplateApptsStartEvery);
SetStartTimeSelect(i, oOriginalValue);
oStartDateTime = GetStartTimeSelect(i);
}
else
{

var errorString = ErrorInRow.replace("{0}", iRangeIndex);
alert(errorString);
SetEndTimeSelect(iRangeIndex, oOriginalValue);
}
}


else if ((i <= iRangeIndex) && (oNewDateTime < oEndDateTime))
{
if (i == 0)
{

MergeRow(iRangeIndex);
bMergeFirst = true;
}
else
{
MergeRow(i);
i--;
}
iRangeIndex--;
oStartDateTime = GetStartTimeSelect(i);
oEndDateTime = GetEndTimeSelect(i);
}

else if ((i > iRangeIndex) && (oNewDateTime >= oEndDateTime))
{
MergeRow(i);
i--;
SetEndTimeSelect(i, oNewDateTime);
if (iRangeIndex < iMaxRange - 1)
{
SetStartTimeSelect(i + 1, oNewDateTime);
}
oStartDateTime = GetStartTimeSelect(i);
oEndDateTime = GetEndTimeSelect(i);
}

if ((i == iRangeIndex) && (oEndDateTime <= oStartDateTime))
{
MergeRow(i);
iRangeIndex--;
}
}
}
else
{
SetEndTimeSelect(iRangeIndex, oOriginalValue);
return;
}
}


else if (iRangeIndex + 1 < iMaxRange)
{
SetStartTimeSelect(iRangeIndex + 1, oNewDateTime);
formUpdateElements(iRangeIndex + 1);
}


MergeValidForMergeRow(iRangeIndex);

MergeValidForMergeRow(iRangeIndex + 1);

formUpdateElements(iRangeIndex);
}

function OnCapacityLostFocus(iRangeIndex)
{
return ValidCapacityRange(iRangeIndex, 0.0001, 999999999);
}


function OnUpdateTotals()
{
var total = 0;
var workTotal = 0;
var breakTotal = 0;

for (i = 0; i < iMaxRange; i++)
{
var rangeDuration = GetRangeDuration(i);

total += rangeDuration;
if (document.getElementById("Type" + i).value == "break")
{
breakTotal += rangeDuration;
}
else
{
workTotal += rangeDuration;
}
}


totalhours.innerHTML = formatDuration(total);
workhours.innerHTML = formatDuration(workTotal);
breakhours.innerHTML = formatDuration(breakTotal);
}

function HidePopup()
{
if(!IsNull(_oPopupMenu))
{
_oPopupMenu.Hide();
}
}


function onMenuButton(iRangeIndex)
{
var oMenuItem;
var sCallBack;

if(event.ctrlKey)
{
HidePopup();
return;
}

_oPopupMenu = new PopupMenu();
with(_oPopupMenu)
{
oMenuItem = document.getElementById("InsertButton" + iRangeIndex);
if (!IsNull(oMenuItem) && (oMenuItem.style.display != "none"))
{
sCallBack = oMenuItem.disabled ? "" : oMenuItem.onclick;
AddMenuItem(oMenuItem.innerHTML, sCallBack, oMenuItem.disabled, "/_imgs/ico_16_split.gif");
}
oMenuItem = document.getElementById("SplitMenuItem" + iRangeIndex);
if (!IsNull(oMenuItem) && (oMenuItem.style.display != "none"))
{
sCallBack = oMenuItem.disabled ? "" : oMenuItem.onclick;
AddMenuItem(oMenuItem.innerHTML, sCallBack, oMenuItem.disabled, "/_imgs/ico_16_split.gif");
}
oMenuItem = document.getElementById("MergeMenuItem" + iRangeIndex);
if (!IsNull(oMenuItem) && (oMenuItem.style.display != "none"))
{
sCallBack = oMenuItem.disabled ? "" : oMenuItem.onclick;
AddMenuItem(oMenuItem.innerHTML, sCallBack, oMenuItem.disabled, "/_imgs/ico_16_remove.gif");
}

var menuButtonWidth = document.getElementById("MenuButton" + iRangeIndex).offsetWidth;
var insertButtonWidth = document.getElementById("InsertButton" + iRangeIndex).offsetWidth;
Show(-1 * insertButtonWidth, event.srcElement.offsetHeight, menuButtonWidth + insertButtonWidth, null, event.srcElement);
}
}



function ToggleCapacity(type, width)
{
var oElement;
var oParent;

for (i = 0; i < iMaxRange; i++)
{
oElement = document.getElementById("Effort" + i);
if (oElement)
{
oParent = oElement.parentElement;
oParent.style.width = width;
var elementHTML = RenderInput(type, oElement.value, "Effort" + i, "OnCapacityLostFocus(" + i + ");",null, EffortRuleName);
oParent.innerHTML = elementHTML;
}

oElement = document.getElementById("MenuButton" + i);
ToggleImage(type, oElement);

oElement = document.getElementById("SplitMenuItem" + i);
ToggleMenuItem(type, oElement);

oElement = document.getElementById("MergeMenuItem" + i);
ToggleMenuItem(type, oElement);
}
}

function ToggleImage(type, oImg)
{
if (!IsNull(oImg) && (oImg.tagName == "IMG") && (oImg.className == "menuButton"))
{
if (type != "hidden")
{
oImg.style.display = "block";
}
else
{
oImg.style.display = "none";
}
}
}

function ToggleMenuItem(type, oSpan)
{
if (!IsNull(oSpan) && (oSpan.tagName == "SPAN"))
{
if (type != "hidden")
{
oSpan.style.display = "block";
}
else
{
oSpan.style.display = "none";
}
}
}


function RenderButton(sOnClick, sId, sName, sClass, sDisplay)
{
return "<button style=\"display:"+sDisplay+"\" class=\""+sClass+"\" tabIndex=\"0\" onclick=\"" + sOnClick + "\" id=\"" + sId + "\">" + sName + "</button>";
}

function RenderImage(sBaseUrl, altTag, sOnClick, sId, sClass, sDisplay)
{
return "<img alt=\""+altTag+"\" src=\""+sBaseUrl+"Off.gif\" style=\"cursor:hand; display:"+sDisplay+"\" class=\""+sClass+"\" tabIndex=\"0\" onclick=\"" + sOnClick + "\" id=\"" + sId + "\"onmouseover=\"this.src='"+sBaseUrl+"On.gif';\" onmouseout=\"this.src='"+sBaseUrl+"Off.gif';\">";
}


function RenderMenuItem(sOnClick, sId, sName, sDisplay)
{
return "<span onclick=\""+sOnClick+"\" id=\""+sId+"\" style=\"display:"+sDisplay+"\">"+sName+"</span>";
}


function RenderInput(sInputType, sValue, sId, sOnBlur, sOnFocus, sTitle)
{
var sEvents = "";
if (!IsNull(sOnBlur))
{
sEvents += "\" onblur=\"" + sOnBlur + "\"";
}
if (!IsNull(sOnFocus))
{
sEvents += " onfocus=\"" + sOnFocus + "\"";
}
if ((!IsNull(sTitle)) && (sInputType != "hidden"))
{
sEvents += "\" title=\"" + sTitle + "\"";
}
return "<input type=\"" + sInputType + "\" tabIndex=\"0\" size=\"2\" value=\"" + sValue + "\" id=\"" + sId + "\"" + sEvents + ">";
}


function RenderTimeTd(TagNameBase, iRangeIndex, oDateTime, sClassName)
{
var oElement;
var oTd = document.createElement("TD");

oTd.className = sClassName;
oTd.id = TagNameBase + "TD" + iRangeIndex;


oTd.innerHTML = BuildTimeSelect(TagNameBase + "Select", iRangeIndex, oDateTime);

return oTd;
}


function BuildTimeSelect(sIdBase, iRangeIndex, oDefaultTime)
{
var oElement;
if (sIdBase.indexOf("Start") != -1)
{
oElement = RenderStartTimeControl.firstChild.cloneNode(true);
}
else
{
oElement = RenderEndTimeControl.firstChild.cloneNode(true);
}
oElement.id = sIdBase + iRangeIndex;
oElement.OnChange = GetTimeSelectEventHandlerNameFromClass(sIdBase, iRangeIndex);
oElement.oninitcomplete = "onSelectInitDataValue('"+oElement.id+"')";

oElement.initialvalue = FormatUtcDate(oDefaultTime);

oElement.initialDisableInit = "false";
return oElement.outerHTML;
}



function GetRangeTime(sIdBase, iRangeIndex)
{
var oElement = document.getElementById(sIdBase + iRangeIndex);
if (oElement != null)
{
if (IsNull(oElement.DataValue))
{

return ParseClientDateTime(oElement.initialvalue);
}
else
{
return new Date(oElement.DataValue);
}
}
else
{
var errorString = ErrorInRow.replace("{0}", sIdBase + iRangeIndex);
alert(errorString);
}
}

function GetStartTimeSelect(iRangeIndex)
{
return GetRangeTime("StartTimeSelect", iRangeIndex);
}

function GetEndTimeSelect(iRangeIndex)
{
var oEndTime = GetRangeTime("EndTimeSelect", iRangeIndex);
if ((oEndTime.getHours() == 0) && (oEndTime.getMinutes() == 0))
{
if (getDatePartDateObject(oEndTime).toString() == getDatePartDateObject(_oBaseDate).toString())
{

oEndTime.setHours(24);
}
}
else if (_oBaseDate != null)
{
oEndTime = 	newDate(_oBaseDate.getFullYear(), _oBaseDate.getMonth(), _oBaseDate.getDate(), oEndTime.getHours(), oEndTime.getMinutes(), oEndTime.getSeconds());
}

return oEndTime;
}

function SetRangeTime(sIdBase, iRangeIndex, oDateTime)
{
var oElement = document.getElementById(sIdBase + iRangeIndex);
if (oElement != null)
{
if (IsNull(oElement.DataValue))
{

oElement.initialvalue = FormatUtcDate(oDateTime);
}
oElement.DataValue = oDateTime;
}
else
{
var errorString = ErrorInRow.replace("{0}", sIdBase + iRangeIndex);
alert(errorString);
}
}

function SetStartTimeSelect(iRangeIndex, oDateTime)
{
SetRangeTime("StartTimeSelect", iRangeIndex, oDateTime);
var oTd = document.getElementById("StartTimeTD" + iRangeIndex);
oTd.dataValue = oTd.dataValue;
}

function SetEndTimeSelect(iRangeIndex, oDateTime)
{
SetRangeTime("EndTimeSelect", iRangeIndex, oDateTime);
var oTd = document.getElementById("EndTimeTD" + iRangeIndex);
oTd.dataValue = oTd.dataValue;

}

function GetRangeDuration(iRangeIndex)
{
var oStartDateTime = GetStartTimeSelect(iRangeIndex);
var oEndDateTime = GetEndTimeSelect(iRangeIndex);

return (oEndDateTime - oStartDateTime)/60000;
}

function RenumberTimeSelect(sIdBase, OldRangeIndex, NewRangeIndex)
{
var oElement;
oElement = document.getElementById(sIdBase + OldRangeIndex);
if (oElement != null)
{
oElement.id = sIdBase + NewRangeIndex;
oElement.OnChange = new Function(GetTimeSelectEventHandlerNameFromClass(sIdBase, NewRangeIndex));
}
else
{
var errorString = ErrorInRow.replace("{0}", sIdBase + iRangeIndex);
alert(errorString);
}
}

function RenumberStartTimeSelect(OldRangeIndex, NewRangeIndex)
{
RenumberTimeSelect("StartTimeSelect", OldRangeIndex, NewRangeIndex);
}

function RenumberEndTimeSelect(OldRangeIndex, NewRangeIndex)
{
RenumberTimeSelect("EndTimeSelect", OldRangeIndex, NewRangeIndex);
}

function GetTimeSelectEventHandlerNameFromClass(sIdBase, iRangeIndex)
{
return "On" + sIdBase.replace("Select", "") + "Change(" + iRangeIndex + ")";
}

function CheckEndNotCloseToMidnight(oNewValue)
{
var oNextEndDateTime = new Date(oNewValue.toString());
oNextEndDateTime.setMinutes(oNextEndDateTime.getMinutes() + TemplateApptsStartEvery);
return oNewValue.getDay() == oNextEndDateTime.getDay();
}

function CheckStartNotCloseToMidnight(oNewValue)
{
var oNextStartDateTime = new Date(oNewValue.toString());
oNextStartDateTime.setMinutes(oNextStartDateTime.getMinutes() - TemplateApptsStartEvery);
return oNewValue.getDay() == oNextStartDateTime.getDay();
}

function getDatePartDateObject(oDateTime)
{
return newDate(oDateTime.getFullYear(), oDateTime.getMonth(), oDateTime.getDate());
}


function FormatShortDateTime(oDateTime)
{
return timeToString(oDateTime, 0);
}


function ParseClientDateTime(sDateTime)
{
return ParseUtcDate(sDateTime);
}


function ParseClientTimeSpan(sDateTime)
{
var Minutes = (parseInt(sDateTime.substr(0, 2), 10) * 60) + parseInt(sDateTime.substr(3, 2), 10);
return Minutes;
}



function GetSplitTime(oStartDateTime, oEndDateTime)
{

var TimeDiff = oEndDateTime - oStartDateTime;
Minutes = TimeDiff/60000;


Minutes /= 2;


iRemainder = Minutes % TemplateApptsStartEvery;
Minutes -= iRemainder;
if (iRemainder > TemplateApptsStartEvery / 2)
{
Minutes += TemplateApptsStartEvery;
}

oStartDateTime.setMinutes(oStartDateTime.getMinutes() + Minutes);
return oStartDateTime;
}


function GetBreakTime(iRangeIndex, iDuration)
{
var oBreakTime = GetStartTimeSelect(iRangeIndex);


if (!iDuration)
{
var iRangeDuration = GetRangeDuration(iRangeIndex);
if (iRangeDuration < 90)
{
oBreakTime.setMinutes(oBreakTime.getMinutes() + TemplateApptsStartEvery);
}
else
{
oBreakTime.setMinutes(oBreakTime.getMinutes() + 30);
}
}
else
{
oBreakTime.setMinutes(oBreakTime.getMinutes() + iDuration);
}
return oBreakTime;
}


function CalculateEndTime(oDateTime, iDuration)
{
oDateTime.setMinutes(oDateTime.getMinutes() + iDuration);

return FormatShortDateTime(oDateTime);
}



function LoadFromXml()
{
var oChildNode;
var type;
var start;
var duration;
var effort;
var toggleEffort = false;
var lineBeforeBreak = false;
var lineAfterBreak = false;
var oXml = CreateXmlDocument(false);
oXml.loadXML(TimeSheetForm.serverData.value);

var childNodes = oXml.firstChild.childNodes;

if (childNodes.length > 0)
{
for (var i = 0; i < childNodes.length; i++)
{
oChildNode = childNodes.item(i);
type = oChildNode.selectSingleNode("type").text;
start = new Date(ParseClientDateTime(oChildNode.selectSingleNode("start").text));
duration = ParseClientTimeSpan(oChildNode.selectSingleNode("duration").text);
if ((type == "effort") && (oChildNode.firstChild.nextSibling.nextSibling.nextSibling))
{
effort = oChildNode.selectSingleNode("effort").text;
if (effort != DefaultEffort)
{
toggleEffort = true;
}
lineBeforeBreak = true;
}
else
{
effort = DefaultEffort;
lineBeforeBreak = false;
}
if (!lineBeforeBreak)
{
InsertRule(iMaxRange-1, start, duration);
lineAfterBreak = true;
}
else
{
if (i > 0)
{
if (lineAfterBreak)
{
MergeRow(iMaxRange-1);
lineAfterBreak = false;
}
RenderRow(type, iMaxRange-1, "cellBottom", start, effort);
iMaxRange++;
}
else
{
if (effort != DefaultEffort)
{
document.getElementById("Effort0").value = effort;
}
}

}
}
}

if (toggleEffort)
{
OnEffortToggle();
}
}



function capacityFormValue(iRangeIndex)
{
var capacity = null;
try { capacity = parseFloat(RemoveFormatting(Trim(document.getElementById("Effort" + iRangeIndex).value))); }catch(e){}
return capacity;
}


function isFloatingNumber(fValue)
{


if (IsNull(fValue))
{
return false;
}
if (fValue.toString().search(/^\d*\.?\d*$/) != -1)
{
return true;
}
return false;
}

function isInRange (nValue, lowerlimit, upperlimit)
{
if (nValue == "")
{
return false;
}
if ((nValue < lowerlimit) || (nValue > upperlimit) )
{
return false;
}
return true;
}

function ValidCapacityRange(index,low,high)
{
var error = false;
var fValue = null;
var field = TimeSheetForm.elements["Effort" + index];

fValue = capacityFormValue(index);

if (IsNull(fValue))
{
error = true;
}


if (fValue != "" )
{
if (!isFloatingNumber(fValue))
{
error = true;
}
if (!isInRange(fValue,low,high))
{
error = true;
}
}
else
{
error = true;
}
if (error)
{
var sMsg = formatString(ValidRangeMessage, EffortRuleName, low, high);
alert(sMsg);
if (EnableEffort)
{
field.focus();
field.select();
}
return false;
}
return true;
}